package com.sh.personnel.config.swagger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * 配置swagger2
 * @author Administrator
 * @date 2020/01/17 0:36
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {

    @Bean
    public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                // 设置swagger-ui.html页面上的一些元素信息
                .apiInfo(metaData())
                .select()
                // 扫描的包路径
                .apis(RequestHandlerSelectors.basePackage("com.sh.persionnel.controller"))
                // 定义要生成api文档的url路径规则
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 接口文档介绍
     * @return
     */
    private ApiInfo metaData() {
        return new ApiInfoBuilder()
                // 标题
                .title("人事权限管理系统-API文档")
                // 描述
                .description("人事权限管理系统")
                // 作者信息
                .contact(new Contact("SIHAOLIN","", "15089132169@163.com"))
                // 版本
                .version("1.0.0")
                .build();
    }

    /**
     * 配置静态资源路径映射
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}
